<#assign tit>会籍停转补</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  function chgtp(t) {
    document.getElementById("tr_c1").style.display = t!=3?"":"none";
    document.getElementById("tr_c2").style.display = t==3?"":"none";
    clearData(0);

    document.getElementById("tbd_c1").style.display = t==1?"":"none";
    document.getElementById("tbd_c2").style.display = t==2?"":"none";
    document.getElementById("tbd_c3").style.display = t==3?"":"none";
  }
  var _cinfo = null;
  function getcontr(c, t) {
    if(c!="") {
      getJson("getContract?code="+c+"&tp="+t, function(data){
        if(data.ret) {
          var mc = data.data;
          var fm = getEl("fm_input");
          var st = fm.saleType.value;
          fm.contractCode.value = mc.contractCode;
          fm.cardCode.value = mc.cardCode;
          fm.memberCode.value = mc.memberCode;
          fm.memberName.value = mc.memberName;
          fm.contractType.value = mc.cardType+" - "+mc.cardTypeName;
          fm.contractBegin.value = getDateStr(new Date(mc.beginTime));
          fm.contractEnd.value = getDateStr(new Date(mc.endTime));
          fm.contractAmount.value = formatMoney(mc.factPrice);
          fm.outAmount.value = formatMoney(mc.leftAmount);
          fm.shopName.value = mc.shopCode+" - "+mc.shopName;
          fm.addFee.value = formatMoney(st==1?mc.stopFee:(st==2?mc.chgShopFee:mc.mendFee));
          fm.earnestId.value = "";
          fm.earnestAmount.value = "";
          _cinfo = mc;
          chgstm();
          setPayAmount();
        } else {
          window.alert(data.msg);
          clearData(t);
        }
      });
    }
  }

  function clearData(t) {
    var fm = getEl("fm_input");
    if(t!=1) fm.contractCode.value = "";
    if(t!=2) fm.cardCode.value = "";
    fm.memberCode.value = "";
    fm.memberName.value = "";
    fm.contractType.value = "";
    fm.contractBegin.value = "";
    fm.contractEnd.value = "";
    fm.contractAmount.value = "";
    fm.outAmount.value = "";
    fm.shopName.value = "";
    fm.earnestId.value = "";
    fm.earnestAmount.value = "";
    fm.addFee.value = "";
    fm.payAmount.value = "";
    fm.stopMonth.value = "";
    fm.beginStopTime.value = "";
    fm.endStopTime.value = "";
    fm.newShopCode.value = "";
    fm.newCardCode.value = "";
    fm.note.value = "";
    _cinfo = null;
  }

  function seldj() {
    var fm = getEl("fm_input");
    var url = "getEarnestList?type=3";
    if(fm.memberCode.value=="") {
      window.alert("请先输入"+(fm.saleType.value=="3"?"会员卡号":"合同编号"));
      return;
    }
    url += "&memberCode="+fm.memberCode.value;
    openWin(url, 620, 400);
  }
  function seldjOk(vv) {
    var ss = vv.split(",");
    var fm = getEl("fm_input");
    fm.earnestId.value = ss[0];
    fm.earnestAmount.value = ss[1];
    setPayAmount();
  }

  function chgstm() {
    var fm = getEl("fm_input");
    if(fm.saleType.value!="1") return;
    var m = parseInt(fm.stopMonth.value,10);
    if(isNaN(m) || m<1) {
      m = 1;
      fm.stopMonth.value = m;
    }
    var dt = parseDate(fm.beginStopTime.value);
    if(dt==null) {
      dt = new Date();
      fm.beginStopTime.value = getDateStr(dt);
    }
    dt.addMonth(m);
    fm.endStopTime.value = getDateStr(dt);

    fm.addFee.value = formatMoney(Math.round(_cinfo.stopFee*m));
    setPayAmount();
  }

  function setPayAmount() {
    var fm = getEl("fm_input");
    if(fm.addFee.value=="") {
      fm.payAmount.value = "";
      return;
    }
    var m1 = parseFloat(fm.addFee.value);
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    var m = m1-m2;
    if(m<0) m = 0;
    fm.payAmount.value = formatMoney(m);
  }

  function selCard() {
    openWin("getCardList", 500, 400);
  }
  function selCardOk(vv) {
    getEl("fm_input").newCardCode.value = vv;
  }
  function chkCard(ipt) {
    if(ipt.value!="") {
      getJson("getCardNew?cardCode="+ipt.value, function(data){
        if(data.ret) {
          ipt.value = data.data.cardCode;
        } else {
          window.alert(data.msg);
        }
      });
    }
  }

  function save(fm) {
    if(!fm) fm = getEl("fm_input");
    if(!_cinfo) {
      if(fm.saleType.value=="3")
        window.alert("请输入会员卡号");
      else
        window.alert("请输入合同编号");
      return;
    }

    var mm = fm.payAmount.value;
    post(fm, function(){
      openWin("getPay?money="+mm, 500, 400);
      return false;
    });
    return false;
  }
  function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    var url;
    var tp = fm.saleType.value;
    if(tp==1)
      url = "_stoped";
    else if(tp==2)
      url = "_chgShop";
    else
      url = "_chgCard";
    postForm(url, fm, saveRet);
  }
  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) getEl("fm_input").reset(); //window.location = "print?id="+data.data;
  }
</script>
<div style="width:600px;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value="">
    <div class="kbox">
      <table class="input w4">
        <tr>
          <th width="85">选择类型</th>
          <td width="160"><select name="saleType" id="sl_saleType" onchange="chgtp(this.value);">
            <option value="1">停卡</option>
            <option value="2">转店</option>
            <option value="3">补卡</option>
          </select></td>
          <th width="85"></th>
          <td width="160"><!--label><input type="checkbox" class="cb" name="signStatus" value="0">不使用手写签名</label--></td>
        </tr>
        <tr id="tr_c1">
          <th>合同编号</th>
          <td><input type="text" name="contractCode" value="" onchange="getcontr(this.value,1);" maxlength="20"></td>
          <th></th>
          <td></td>
        </tr>
        <tr id="tr_c2" style="display:none;">
          <th>会员卡号</th>
          <td><input type="text" name="cardCode" value="" onchange="getcontr(this.value,2);" maxlength="20"></td>
          <th></th>
          <td></td>
        </tr>
        <tbody style="border:3px solid #ffefce;">
          <tr>
            <th>会员姓名</th>
            <td><input type="text" name="memberName" value="" maxlength="10" class="disable" readonly></td>
            <th>会员编号</th>
            <td><input type="text" name="memberCode" value="" class="disable" readonly></td>
          </tr>
          <tr>
            <th>原会籍类型</th>
            <td colspan="3"><input type="text" name="contractType" value="" class="w2 disable" readonly></td>
          </tr>
          <tr>
            <th>合同起日</th>
            <td><input type="text" name="contractBegin" value="" class="date disable" maxlength="10" readonly></td>
            <th>合同迄日</th>
            <td><input type="text" name="contractEnd" value="" class="date disable" maxlength="10" readonly></td>
          </tr>
          <tr>
            <th>合同价值</th>
            <td><input type="text" name="contractAmount" value="" class="disable" readonly></td>
            <th>剩余价值</th>
            <td><input type="text" name="outAmount" value="" class="disable" readonly></td>
          </tr>
        </tbody>

        <tr>
          <th>定金编号</th>
          <td><input type="text" name="earnestId" value="" style="width:90px;" class="disable" readonly>
            <input type="button" class="opwin" value="..." onclick="seldj();"/></td>
          <th>定金金额</th>
          <td><input type="text" name="earnestAmount" value="" class="disable" readonly></td>
        </tr>

        <tbody id="tbd_c1" style="display:;">
          <tr>
            <th>停卡月数</th>
            <td><input type="text" name="stopMonth" value="" onchange="chgstm();"></td>
            <th></th>
            <td></td>
          </tr>
          <tr>
            <th>停卡开始日</th>
            <td><input type="text" name="beginStopTime" value="" onchange="chgstm();" class="date" maxlength="10" onfocus="cal(this);"></td>
            <th>停卡结束日</th>
            <td><input type="text" name="endStopTime" value="" class="date disable" maxlength="10" readonly></td>
          </tr>
        </tbody>
        <tbody id="tbd_c2" style="display:none;">
          <tr>
            <th>转出门店</th>
            <td colspan="3"><input type="text" name="shopName" value="" class="w2 disable" readonly></td>
          </tr>
          <tr>
            <th>转入门店</th>
            <td colspan="3"><select name="newShopCode" class="w2"><option value="">--请选择--</option><#if shopName??><#list shopName as t><option value="${t.id!}">${t.id!} - ${t.name!}</option></#list></#if></select></td>
          </tr>
        </tbody>
        <tbody id="tbd_c3" style="display:none;">
          <tr>
            <th>新会员卡号</th>
            <td colspan="3"><input type="text" name="newCardCode" value="" onchange="chkCard(this);" maxlength="20">
              <input type="button" class="opwin" value="..." onclick="selCard();"/></td>
          </tr>
        </tbody>

        <tr>
          <th>手续费</th>
          <td><input type="text" name="addFee" value="" class="disable" readonly></td>
          <th>应收金额</th>
          <td><input type="text" name="payAmount" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>备注</th>
          <td colspan="3"><textarea name="note"></textarea></td>
        </tr>
      </table>
    </div>
    <div class="button"><input type="button" value="确认并提交" onclick="save();"></div>
  </form>
</div>

</@page.html>